﻿namespace Windows32
{
    using System;
    using System.Collections.Specialized;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Text;

    public class WinInet : IDisposable
    {
        private int hInternetHandle = 0;
        public const uint HTTP_ADDREQ_FLAG_ADD = 0x20000000;
        public const uint HTTP_ADDREQ_FLAG_ADD_IF_NEW = 0x10000000;
        public const uint HTTP_ADDREQ_FLAG_COALESCE = 0x40000000;
        public const uint HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA = 0x40000000;
        public const uint HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON = 0x1000000;
        public const uint HTTP_ADDREQ_FLAG_REPLACE = 0x80000000;
        public const uint HTTP_ADDREQ_FLAGS_MASK = 0xffff0000;
        public const uint HTTP_ADDREQ_INDEX_MASK = 65535;
        public const int HTTP_QUERY_ACCEPT = 24;
        public const int HTTP_QUERY_ACCEPT_CHARSET = 25;
        public const int HTTP_QUERY_ACCEPT_ENCODING =26;
        public const int HTTP_QUERY_ACCEPT_LANGUAGE = 27;
        public const int HTTP_QUERY_ACCEPT_RANGES = 42;
        public const int HTTP_QUERY_ALLOW = 7;
        public const int HTTP_QUERY_AUTHORIZATION = 0x1c;
        public const int HTTP_QUERY_CONNECTION = 0x17;
        public const int HTTP_QUERY_CONTENT_DESCRIPTION = 4;
        public const int HTTP_QUERY_CONTENT_DISPOSITION = 0x2f;
        public const int HTTP_QUERY_CONTENT_ENCODING = 0x1d;
        public const int HTTP_QUERY_CONTENT_ID = 3;
        public const int HTTP_QUERY_CONTENT_LANGUAGE = 6;
        public const int HTTP_QUERY_CONTENT_LENGTH = 5;
        public const int HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2;
        public const int HTTP_QUERY_CONTENT_TYPE = 1;
        public const int HTTP_QUERY_COOKIE = 0x2c;
        public const int HTTP_QUERY_COST = 15;
        public const int HTTP_QUERY_DATE = 9;
        public const int HTTP_QUERY_DERIVED_FROM = 14;
        public const int HTTP_QUERY_EXPIRES = 10;
        public const int HTTP_QUERY_FORWARDED = 30;
        public const int HTTP_QUERY_FROM = 0x1f;
        public const int HTTP_QUERY_IF_MODIFIED_SINCE = 0x20;
        public const int HTTP_QUERY_LAST_MODIFIED = 11;
        public const int HTTP_QUERY_LINK = 0x10;
        public const int HTTP_QUERY_LOCATION = 0x21;
        public const int HTTP_QUERY_MESSAGE_ID = 12;
        public const int HTTP_QUERY_MIME_VERSION = 0;
        public const int HTTP_QUERY_ORIG_URI = 0x22;
        public const int HTTP_QUERY_PRAGMA = 0x11;
        public const int HTTP_QUERY_PROXY_AUTHENTICATE = 0x29;
        public const int HTTP_QUERY_PUBLIC = 8;
        public const int HTTP_QUERY_RAW_HEADERS = 0x15;
        public const int HTTP_QUERY_RAW_HEADERS_CRLF = 0x16;
        public const int HTTP_QUERY_REFERER = 0x23;
        public const int HTTP_QUERY_REFRESH = 0x2e;
        public const int HTTP_QUERY_REQUEST_METHOD = 0x2d;
        public const int HTTP_QUERY_RETRY_AFTER = 0x24;
        public const int HTTP_QUERY_SERVER = 0x25;
        public const int HTTP_QUERY_SET_COOKIE = 0x2b;
        public const int HTTP_QUERY_STATUS_CODE = 0x13;
        public const int HTTP_QUERY_STATUS_TEXT = 20;
        public const int HTTP_QUERY_TITLE = 0x26;
        public const int HTTP_QUERY_URI = 13;
        public const int HTTP_QUERY_USER_AGENT = 0x27;
        public const int HTTP_QUERY_VERSION = 0x12;
        public const int HTTP_QUERY_WWW_AUTHENTICATE = 40;
        public const int HTTP_STATUS_ACCEPTED = 0xca;
        public const int HTTP_STATUS_AMBIGUOUS = 300;
        public const int HTTP_STATUS_BAD_GATEWAY = 0x1f6;
        public const int HTTP_STATUS_BAD_METHOD = 0x195;
        public const int HTTP_STATUS_BAD_REQUEST = 400;
        public const int HTTP_STATUS_CONFLICT = 0x199;
        public const int HTTP_STATUS_CONTINUE = 100;
        public const int HTTP_STATUS_CREATED = 0xc9;
        public const int HTTP_STATUS_DENIED = 0x191;
        public const int HTTP_STATUS_FORBIDDEN = 0x193;
        public const int HTTP_STATUS_GATEWAY_TIMEOUT = 0x1f8;
        public const int HTTP_STATUS_GONE = 410;
        public const int HTTP_STATUS_LENGTH_REQUIRED = 0x19b;
        public const int HTTP_STATUS_MOVED = 0x12d;
        public const int HTTP_STATUS_NO_CONTENT = 0xcc;
        public const int HTTP_STATUS_NONE_ACCEPTABLE = 0x196;
        public const int HTTP_STATUS_NOT_FOUND = 0x194;
        public const int HTTP_STATUS_NOT_MODIFIED = 0x130;
        public const int HTTP_STATUS_NOT_SUPPORTED = 0x1f5;
        public const int HTTP_STATUS_OK = 200;
        public const int HTTP_STATUS_PARTIAL = 0xcb;
        public const int HTTP_STATUS_PARTIAL_CONTENT = 0xce;
        public const int HTTP_STATUS_PAYMENT_REQ = 0x192;
        public const int HTTP_STATUS_PRECOND_FAILED = 0x19c;
        public const int HTTP_STATUS_PROXY_AUTH_REQ = 0x197;
        public const int HTTP_STATUS_REDIRECT = 0x12e;
        public const int HTTP_STATUS_REDIRECT_KEEP_VERB = 0x133;
        public const int HTTP_STATUS_REDIRECT_METHOD = 0x12f;
        public const int HTTP_STATUS_REQUEST_TIMEOUT = 0x198;
        public const int HTTP_STATUS_REQUEST_TOO_LARGE = 0x19d;
        public const int HTTP_STATUS_RESET_CONTENT = 0xcd;
        public const int HTTP_STATUS_SERVER_ERROR = 500;
        public const int HTTP_STATUS_SERVICE_UNAVAIL = 0x1f7;
        public const int HTTP_STATUS_SWITCH_PROTOCOLS = 0x65;
        public const int HTTP_STATUS_UNSUPPORTED_MEDIA = 0x19f;
        public const int HTTP_STATUS_URI_TOO_LONG = 0x19e;
        public const int HTTP_STATUS_USE_PROXY = 0x131;
        public const int HTTP_STATUS_VERSION_NOT_SUP = 0x1f9;
        private int iContentLength = 0;
        public const int INTERNET_FLAG_ASYNC = 0x10000000;
        public const int INTERNET_FLAG_CACHE_ASYNC = 0x80;
        public const int INTERNET_FLAG_CACHE_IF_NET_FAIL = 0x10000;
        public const int INTERNET_FLAG_DONT_CACHE = 0x4000000;
        public const int INTERNET_FLAG_EXISTING_CONNECT = 0x20000000;
        public const int INTERNET_FLAG_FORMS_SUBMIT = 0x40;
        public const int INTERNET_FLAG_FROM_CACHE = 0x1000000;
        public const int INTERNET_FLAG_HYPERLINK = 0x400;
        public const int INTERNET_FLAG_IGNORE_CERT_CN_INVALID = 0x1000;
        public const int INTERNET_FLAG_IGNORE_CERT_DATE_INVALID = 0x2000;
        public const int INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP = 0x8000;
        public const int INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS = 0x4000;
        public const int INTERNET_FLAG_KEEP_CONNECTION = 0x400000;
        public const int INTERNET_FLAG_MAKE_PERSISTENT = 0x2000000;
        public const int INTERNET_FLAG_MUST_CACHE_REQUEST = 0x10;
        public const int INTERNET_FLAG_NEED_FILE = 0x10;
        public const int INTERNET_FLAG_NO_AUTH = 0x40000;
        public const int INTERNET_FLAG_NO_AUTO_REDIRECT = 0x200000;
        public const int INTERNET_FLAG_NO_CACHE_WRITE = 0x4000000;
        public const int INTERNET_FLAG_NO_COOKIES = 0x80000;
        public const int INTERNET_FLAG_NO_UI = 0x200;
        public const int INTERNET_FLAG_OFFLINE = 0x1000000;
        public const int INTERNET_FLAG_PASSIVE = 0x8000000;
        public const int INTERNET_FLAG_PRAGMA_NOCACHE = 0x100;
        public const int INTERNET_FLAG_RAW_DATA = 0x40000000;
        public const int INTERNET_FLAG_READ_PREFETCH = 0x100000;
        public const uint INTERNET_FLAG_RELOAD = 0x80000000;
        public const int INTERNET_FLAG_RESYNCHRONIZE = 0x800;
        public const int INTERNET_FLAG_SECURE = 0x800000;
        private int iReserveLength = 0;
        private NameValueCollection myHeads = new NameValueCollection();
        private System.Uri myUri = null;
        private string strCookies = null;
        private string strURLString = null;
        public bool ThrowException = true;

        public event EventHandler Downloading;

        public void Close()
        {
            if (this.hInternetHandle != 0)
            {
                InternetCloseHandle(this.hInternetHandle);
                this.hInternetHandle = 0;
            }
        }

        public void Dispose()
        {
            if (this.hInternetHandle != 0)
            {
                InternetCloseHandle(this.hInternetHandle);
                this.hInternetHandle = 0;
            }
        }

        public static byte[] GetBytesFromPost(string strURL, byte[] bytFormData)
        {
            int num6;
            string sHeaders = "Content-Type: application/x-www-form-urlencoded";
            string str2 = "Accept: */*";
            byte[] buffer = null;
            System.Uri uri = new System.Uri(strURL);
            int iInternetSession = InternetOpen("htinc", 0, null, null, 0x80000000);
            if (iInternetSession == 0)
            {
                return buffer;
            }
            int iInet = InternetConnect(iInternetSession, uri.Host, uri.Port, null, null, 3, 0, 0);
            if (iInet == 0)
            {
                goto Label_011E;
            }
            char[] chArray = str2.ToCharArray();
            string s = uri.AbsolutePath + "\0";
            byte[] bytes = Encoding.GetEncoding(0x3a8).GetBytes(s);
            int hHttpRequest = HttpOpenRequest((long) iInet, s, "POST\0", null, null, null, 0L, 1);
            if (hHttpRequest == 0)
            {
                goto Label_0116;
            }
            if ((HttpSendRequest(hHttpRequest, sHeaders, sHeaders.Length, bytFormData, bytFormData.Length) == 0) || !IsHttpStatusOK(hHttpRequest))
            {
                goto Label_010E;
            }
            MemoryStream stream = new MemoryStream();
            byte[] sBuffer = new byte[0x800];
            int lNumberOfBytesRead = 0;
        Label_00D2:
            num6 = InternetReadFile(hHttpRequest, sBuffer, 0x800, ref lNumberOfBytesRead);
            if ((lNumberOfBytesRead > 0) && (num6 != 0))
            {
                stream.Write(sBuffer, 0, lNumberOfBytesRead);
                goto Label_00D2;
            }
            buffer = stream.ToArray();
            stream.Close();
        Label_010E:
            InternetCloseHandle(hHttpRequest);
        Label_0116:
            InternetCloseHandle(iInet);
        Label_011E:
            InternetCloseHandle(iInternetSession);
            return buffer;
        }

        public static string GetHttpErrorText()
        {
            byte[] buffer = new byte[0xfbc];
            int length = buffer.Length;
            int errorCode = 0;
            if (InternetGetLastResponseInfo(ref errorCode, buffer, ref length))
            {
                return new string(Encoding.Unicode.GetChars(buffer, 0, length));
            }
            return null;
        }

        public static string GetHttpErrorText(int intErrorCode)
        {
            switch (intErrorCode)
            {
                case 0x2ee1:
                    return "No more Internet handles can be allocated";

                case 0x2ee2:
                    return "The operation timed out";

                case 0x2ee3:
                    return "The server returned extended information";

                case 0x2ee4:
                    return "An internal error occurred in the Microsoft Internet extensions";

                case 0x2ee5:
                    return "The URL is invalid";

                case 0x2ee6:
                    return "The URL does not use a recognized protocol";

                case 0x2ee7:
                    return "The server name or address could not be resolved";

                case 0x2ee8:
                    return "A protocol with the required capabilities was not found";

                case 0x2ee9:
                    return "The option is invalid";

                case 0x2eea:
                    return "The length is incorrect for the option type";

                case 0x2eeb:
                    return "The option value cannot be set";

                case 0x2eec:
                    return "Microsoft Internet Extension support has been shut down";

                case 0x2eed:
                    return "The user name was not allowed";

                case 0x2eee:
                    return "The password was not allowed";

                case 0x2eef:
                    return "The login request was denied";

                case 0x2ef1:
                    return "The operation has been canceled";

                case 0x2ef2:
                    return "The supplied handle is the wrong type for the requested operation";

                case 0x2ef3:
                    return "The handle is in the wrong state for the requested operation";

                case 0x2ef4:
                    return "The request cannot be made on a Proxy session";

                case 0x2ef5:
                    return "The registry value could not be found";

                case 0x2ef6:
                    return "The registry parameter is incorrect";

                case 0x2ef7:
                    return "Direct Internet access is not available";

                case 0x2ef8:
                    return "No context value was supplied";

                case 0x2ef9:
                    return "No status callback was supplied";

                case 0x2efa:
                    return "There are outstanding requests";

                case 0x2efb:
                    return "The information format is incorrect";

                case 0x2efc:
                    return "The requested item could not be found";

                case 0x2efd:
                    return "A connection with the server could not be established";

                case 0x2efe:
                    return "The connection with the server was terminated abnormally";

                case 0x2eff:
                    return "The connection with the server was reset";

                case 0x2f00:
                    return "The action must be retried";

                case 0x2f01:
                    return "The proxy request is invalid";

                case 0x2f02:
                    return "User interaction is required to complete the operation";

                case 0x2f04:
                    return "The handle already exists";

                case 0x2f05:
                    return "The date in the certificate is invalid or has expired";

                case 0x2f06:
                    return "The host name in the certificate is invalid or does not match";

                case 0x2f07:
                    return "A redirect request will change a non-secure to a secure connection";

                case 0x2f08:
                    return "A redirect request will change a secure to a non-secure connection";

                case 0x2f09:
                    return "Mixed secure and non-secure connections";

                case 0x2f0a:
                    return "Changing to non-secure post";

                case 0x2f0b:
                    return "Data is being posted on a non-secure connection";

                case 0x2f0c:
                    return "A certificate is required to complete client authentication";

                case 0x2f0d:
                    return "The certificate authority is invalid or incorrect";

                case 0x2f0e:
                    return "Client authentication has not been correctly installed";

                case 0x2f0f:
                    return "An error has occurred in a Wininet asynchronous thread. You may need to restart";

                case 0x2f10:
                    return "The protocol scheme has changed during a redirect operaiton";

                case 0x2f11:
                    return "There are operations awaiting retry";

                case 0x2f12:
                    return "The operation must be retried";

                case 0x2f13:
                    return "There are no new cache containers";

                case 0x2f14:
                    return "A security zone check indicates the operation must be retried";

                case 0x2f4a:
                    return "The requested operation is invalid";

                case 0x2f4e:
                    return "There is already an FTP request in progress on this session";

                case 0x2f4f:
                    return "The FTP session was terminated";

                case 0x2f50:
                    return "FTP Passive mode is not available";

                case 0x2f62:
                    return "A gopher protocol error occurred";

                case 0x2f63:
                    return "The locator must be for a file";

                case 0x2f64:
                    return "An error was detected while parsing the data";

                case 0x2f65:
                    return "There is no more data";

                case 0x2f66:
                    return "The locator is invalid";

                case 0x2f67:
                    return "The locator type is incorrect for this operation";

                case 0x2f68:
                    return "The request must be for a gopher+ item";

                case 0x2f69:
                    return "The requested attribute was not found";

                case 0x2f6a:
                    return "The locator type is not recognized";

                case 0x2f76:
                    return "The requested header was not found";

                case 0x2f77:
                    return "The server does not support the requested protocol level";

                case 0x2f78:
                    return "The server returned an invalid or unrecognized response";

                case 0x2f79:
                    return "The supplied HTTP header is invalid";

                case 0x2f7a:
                    return "The request for a HTTP header is invalid";

                case 0x2f7b:
                    return "The HTTP header already exists";

                case 0x2f7c:
                    return "The HTTP redirect request failed";

                case 0x2f7d:
                    return "An error occurred in the secure channel support";

                case 0x2f7e:
                    return "The file could not be written to the cache";

                case 0x2f7f:
                    return "The TCP/IP protocol is not installed properly";

                case 0x2f80:
                    return "The HTTP request was not redirected";

                case 0x2f81:
                    return "A cookie from the server must be confirmed by the user";

                case 0x2f82:
                    return "A cookie from the server has been declined acceptance";

                case 0x2f83:
                    return "The computer is disconnected from the network";

                case 0x2f84:
                    return "The server is unreachable";

                case 0x2f85:
                    return "The proxy server is unreachable";

                case 0x2f86:
                    return "The proxy auto-configuration script is in error";

                case 0x2f87:
                    return "Could not download the proxy auto-configuration script file";

                case 0x2f88:
                    return "The HTTP redirect request must be confirmed by the user";

                case 0x2f89:
                    return "The supplied certificate is invalid";

                case 0x2f8a:
                    return "The supplied certificate has been revoked";

                case 0x2f8b:
                    return "The Dialup failed because file sharing was turned on and a failure was requested if security check was needed";
            }
            return "not Support ErrorCode ";
        }

        public static string GetHttpStatus(int InfoLevel, int hRequest)
        {
            byte[] buffer = new byte[0x400];
            int length = buffer.Length;
            HttpQueryInfo(hRequest, InfoLevel, buffer, ref length, 0);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < length; i++)
            {
                if (buffer[i] > 0)
                {
                    builder.Append((char) buffer[i]);
                }
            }
            return builder.ToString();
        }

        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern int HttpAddRequestHeaders(int iHttpRequest, string sHeaders, int lHeadersLength, uint lModifiers);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern int HttpOpenRequest(long hInternetSession, string sVerb, string sObjectName, string sVersion, string sReferer, char[] sAcceptTypes, long lFlags, int lContext);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern bool HttpQueryInfo(int hRequest, int iInfoLevel, byte[] Buffer, ref int BufferLength, int pwdIndex);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern int HttpSendRequest(int hHttpRequest, string sHeaders, int lHeadersLength, byte[] sOptional, int lOptionalLength);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern int InternetCloseHandle(int iInet);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern int InternetConnect(int iInternetSession, string sServerName, int iProxyProt, string sUserName, string sPassword, int iService, int lFlags, int iContext);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern bool InternetGetLastResponseInfo(ref int ErrorCode, byte[] Buffer, ref int BufferLength);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern int InternetOpen(string sAgent, int lAccessType, string sProxyName, string sProxyBypass, uint lFlags);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern int InternetOpenUrl(int hOpen, string sUrl, string sHeaders, int lLength, int lFlags, int lContext);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern int InternetReadFile(int hFile, byte[] sBuffer, int lNumbytesToRead, ref int lNumberOfBytesRead);
        [DllImport("wininet.dll", CharSet=CharSet.Auto)]
        public static extern bool InternetSetCookie(string strURL, string strCookieName, string strCookieData);
        public static bool IsHttpStatusOK(int hRequest)
        {
            return (GetHttpStatus(0x13, hRequest) == "200");
        }

        public void Open(string strURL)
        {
            this.Close();
            this.myUri = new System.Uri(strURL);
            this.hInternetHandle = InternetOpen("htinc", 0, null, null, 0x80000000);
            this.myHeads.Set("Host", this.myUri.Host + ":" + this.myUri.Port.ToString());
        }

        public byte[] PostData(byte[] bytPostData)
        {
            bool cancelFlag = false;
            return this.PostData(bytPostData, ref cancelFlag);
        }

        public byte[] PostData(byte[] bytPostData, ref bool CancelFlag)
        {
            this.iReserveLength = 0;
            this.iContentLength = 0;
            byte[] buffer = null;
            Win32APIException exception = null;
            if (CancelFlag)
            {
                return null;
            }
            if (this.hInternetHandle == 0)
            {
                this.hInternetHandle = InternetOpen("htinc", 0, null, null, 0x80000000);
            }
            if (CancelFlag)
            {
                return null;
            }
            if (this.hInternetHandle == 0)
            {
                if (this.ThrowException)
                {
                    exception = new Win32APIException("InternetOpen", this.hInternetHandle);
                    exception.ErrorMsg = GetHttpErrorText();
                    throw exception;
                }
                return buffer;
            }
            int iInet = InternetConnect(this.hInternetHandle, this.myUri.Host, this.myUri.Port, null, null, 3, 0, 0);
            if (CancelFlag)
            {
                InternetCloseHandle(iInet);
                return null;
            }
            if (iInet == 0)
            {
                if (!this.ThrowException)
                {
                    return buffer;
                }
                exception = new Win32APIException("InternetConnect", iInet);
                exception.ErrorMsg = GetHttpErrorText();
                throw exception;
            }
            string sVerb = this.myUri.AbsolutePath + "\0";
            int num2 = HttpOpenRequest((long) iInet, sVerb, "POST\0", null, null, null, 0L, 1);
            if (CancelFlag)
            {
                InternetCloseHandle(num2);
                InternetCloseHandle(iInet);
                return null;
            }
            if (num2 == 0)
            {
                if (this.ThrowException)
                {
                    exception = new Win32APIException("HttpOpenRequest", num2);
                    exception.ErrorMsg = GetHttpErrorText();
                    InternetCloseHandle(iInet);
                    throw exception;
                }
            }
            else
            {
                StringBuilder builder = new StringBuilder();
                if (this.myHeads.Count > 0)
                {
                    for (int i = 0; i < this.myHeads.Count; i++)
                    {
                        if (builder.Length > 0)
                        {
                            builder.Append("\r\n");
                        }
                        builder.Append(this.myHeads.Keys[i]);
                        builder.Append(": ");
                        builder.Append(this.myHeads[i]);
                    }
                }
                string sHeaders = builder.ToString();
                HttpAddRequestHeaders(num2, sHeaders, sHeaders.Length, 0x80000000);
                int num4 = HttpSendRequest(num2, null, 0, bytPostData, bytPostData.Length);
                if (CancelFlag)
                {
                    InternetCloseHandle(num2);
                    InternetCloseHandle(iInet);
                    return null;
                }
                if ((num4 != 0) && IsHttpStatusOK(num2))
                {
                    this.strCookies = GetHttpStatus(0x2b, num2);
                    string httpStatus = GetHttpStatus(5, num2);
                    try
                    {
                        if ((httpStatus != null) && (httpStatus.Trim().Length > 0))
                        {
                            this.iContentLength = Convert.ToInt32(httpStatus);
                        }
                    }
                    catch
                    {
                    }
                    MemoryStream stream = new MemoryStream();
                    byte[] sBuffer = new byte[0x4000];
                    int lNumberOfBytesRead = 0;
                    while (!CancelFlag)
                    {
                        int num6 = InternetReadFile(num2, sBuffer, sBuffer.Length, ref lNumberOfBytesRead);
                        if ((lNumberOfBytesRead > 0) && (num6 != 0))
                        {
                            stream.Write(sBuffer, 0, lNumberOfBytesRead);
                            this.iReserveLength += lNumberOfBytesRead;
                        }
                        else if (((num6 != 0) && (lNumberOfBytesRead == 0)) || (num6 == 0))
                        {
                            break;
                        }
                        if (this.Downloading != null)
                        {
                            this.Downloading(this, null);
                        }
                    }
                    buffer = stream.ToArray();
                    stream.Close();
                }
                InternetCloseHandle(num2);
            }
            InternetCloseHandle(iInet);
            return buffer;
        }

        public bool SetCookie(string strName, string strValue)
        {
            return InternetSetCookie(this.myUri.AbsoluteUri, strName, strValue);
        }

        public int ContentLength
        {
            get
            {
                return this.iContentLength;
            }
        }

        public NameValueCollection Heads
        {
            get
            {
                return this.myHeads;
            }
        }

        public int InternetHandle
        {
            get
            {
                return this.hInternetHandle;
            }
        }

        public int ReserveLength
        {
            get
            {
                return this.iReserveLength;
            }
        }

        public System.Uri Uri
        {
            get
            {
                return this.myUri;
            }
        }

        public string URLString
        {
            get
            {
                return this.strURLString;
            }
        }
    }
}

